package cn.eeepay.boss.action.wetchatPublic;

import java.io.File;
import java.util.List;
import java.util.Map;

import cn.eeepay.boss.system.SystemLog;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.Result;
import cn.eeepay.framework.model.wetchatPublic.PublicAccountMsgTempEntity;
import cn.eeepay.framework.service.unTransactionalImpl.wetchatPublic.PublicAccountMsgTempService;
import cn.eeepay.framework.util.ResultModel;
import cn.eeepay.framework.util.ResponseUtil;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 公众号模板消息表
 *
 * @author quanhz
 * @email
 * @date 2020-07-29 15:31:47
 */
@RestController
@RequestMapping("/publicAccountMsgTemp")
public class PublicAccountMsgTempController {

    private Logger log = LoggerFactory.getLogger(PublicAccountMsgTempController.class);

    @Resource
    private PublicAccountMsgTempService publicAccountMsgTempService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list(
            @Param("page") Page<PublicAccountMsgTempEntity> page, @RequestParam("info") String info) {

        try {
            publicAccountMsgTempService.queryPage(
                    page, JSON.parseObject(info, PublicAccountMsgTempEntity.class));
            return Result.success("查询公众号推送信息成功", page);
        } catch (Exception e) {
            log.error("查询公众号推送信息失败", e);
            return Result.fail("查询公众号推送信息失败");
        }
    }

    @RequestMapping("/queryCondition")
    public Result queryCondition() {

        try {
            Map<String, List<PublicAccountMsgTempEntity>> map =
                    publicAccountMsgTempService.queryCondition();
            return Result.success("获取列表查询条件成功", map);
        } catch (Exception e) {
            log.error("获取列表查询条件失败", e);
            return Result.fail("获取列表插叙条件失败");
        }
    }

    /**
     * 信息
     */
    @RequestMapping("/info")
    public Result info(@RequestParam Integer id) {
        try {
            PublicAccountMsgTempEntity publicAccountMsgTemp = publicAccountMsgTempService.getById(id);
            return Result.success("获取明细成功", publicAccountMsgTemp);
        } catch (Exception e) {
            log.error("获取明细失败", e);
            return Result.fail("获取明细失败");
        }
    }

    /***
     * 下载商户编号模板
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/downloadTemplate")
    @SystemLog(description = "下载模板", operCode = "publicAccountMsgTemp.downloadTemplate")
    public ResultModel downloadMerchantNoTemplate(HttpServletRequest request, HttpServletResponse response) {
        try {
            String filePath = request.getServletContext().getRealPath("/") + "template" + File.separator + "wetchatPubTemplate.xls";
            ResponseUtil.download(response, filePath, "商户信息模板.xlsx");
            return ResultModel.ok();
        } catch (Exception e) {
            log.error("下载模板失败", e);
            return ResultModel.error("下载模板失败");
        }
    }
}
